package com.tywj.panda.driver.details

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.route.RouteParam
import com.tywj.panda.customer.common.http.PDObjectCallback
import com.tywj.panda.customer.common.view.base.MyBaseFragment
import com.tywj.panda.driver.R
import com.tywj.panda.driver.api.DriverApi
import com.tywj.panda.driver.entity.DriverTaskEntity
import com.tywj.panda.driver.entity.DriverTicketEntity
import com.tywj.panda.driver.route.DriverRoute
import kotlinx.android.synthetic.main.driver_fragment_passenger.*


/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/06/22
 *     desc   : 班次详情
 *     version: 2.0.0
 * </pre>
 */
@Route(path = DriverRoute.DRIVER_TASK_PASSENGER)
class DriverPassengerFragment : MyBaseFragment() {

    companion object {
        const val PARAMS_TASK_ENTITY = "taskEntity"
    }

    override fun getLayoutID(): Int = R.layout.driver_fragment_passenger


    private var taskEntity: DriverTaskEntity.DataBean? = null

    private lateinit var headerView: View

    private lateinit var adapter: DriverTicketAdapter

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        taskEntity = RouteParam(this).getParcelable(PARAMS_TASK_ENTITY)
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        swipeRefreshLayout.setColorSchemeResources(R.color.common_yellow_FE)

        activity?.let {
            BarUtil.setStatusBarColor(it, ResourcesUtil.getColor(R.color.common_white))
            BarUtil.setStatusBarLightMode(it.window, true)
        }

        initRecycleView()
    }

    private fun initRecycleView() {

        adapter = DriverTicketAdapter()
        rvList.layoutManager = LinearLayoutManager(context)

        headerView = layoutInflater.inflate(R.layout.driver_check_recyclerview_header, rvList, false)
        adapter.addHeaderView(headerView)
        rvList.adapter = adapter
    }

    override fun initListener() {
        super.initListener()

        swipeRefreshLayout.setOnRefreshListener {
            // 刷新首页数据
            taskEntity?.let {
                loadData(it)
            }
        }

        statusLayout.setOnEmptyLayoutButtonClick {
            // 刷新首页数据
            taskEntity?.let {
                loadData(it)
            }
        }

        statusLayout.setOnErrorLayoutButtonClick {
            // 刷新首页数据
            taskEntity?.let {
                loadData(it)
            }
        }
    }


    override fun loadDataOnCreate() {
        super.loadDataOnCreate()

        taskEntity?.let {
            loadData(it)
        }
    }

    private fun loadData(bean: DriverTaskEntity.DataBean) {
        statusLayout.showLoadingLayout()
        DriverApi.getTicketDetails(bean).attachToLifecycle(this)
            .execute(object : PDObjectCallback<DriverTicketEntity>(DriverTicketEntity::class.java) {
                override fun success(data: DriverTicketEntity) {
                    handleData(data)
                }

                override fun error(code: Int, message: String) {
                    super.error(code, message)
                    statusLayout.showErrorLayoutByCode(code, message)
                }

                override fun onComplete() {
                    super.onComplete()

                    swipeRefreshLayout.isRefreshing = false
                }
            })
    }

    @SuppressLint("SetTextI18n")
    private fun handleData(data: DriverTicketEntity) {
        checkNum.text = "验票数/乘客数：" + data.ticketNumber + "/" + data.passengerNum
        if (data.passengerList.isEmpty()) {
            statusLayout.showEmptyLayout()
        } else {
            statusLayout.showContentLayout()
            adapter.setNewData(data.passengerList)
        }
    }
}